﻿Imports System
Imports System.Globalization
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports System.Workflow.Activities.Rules
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Compiler
Imports System.Workflow.ComponentModel.Design
Imports System.Workflow.Interop

Namespace System.Workflow.Activities.Rules.Design
    Friend NotInheritable Class ConditionBrowserDialog
        Inherits BasicBrowserDialog
        ' Methods
        Public Sub New(ByVal activity As Activity, ByVal name As String)
            MyBase.New(activity, name)
            Dim definitions As RuleDefinitions = ConditionHelper.Load_Rules_DT(activity.Site, Helpers.GetRootActivity(activity))
            If (Not definitions Is Nothing) Then
                Me.declarativeConditionCollection = definitions.Conditions
            End If
            MyBase.InitializeListView(Me.declarativeConditionCollection, name)
        End Sub

        Private Function CreateNewName() As String
            Dim newConditionName As String = Messages.NewConditionName
            Dim num As Integer = 1
            Do While True
                Dim key As String = (newConditionName & num.ToString(CultureInfo.InvariantCulture))
                If Not Me.declarativeConditionCollection.Contains(key) Then
                    Return key
                End If
                num += 1
            Loop
        End Function

        Protected Overrides Function GetObjectName(ByVal ruleObject As Object) As String
            Dim condition As RuleExpressionCondition = [TryCast](ruleObject,RuleExpressionCondition)
            Return condition.Name
        End Function

        Friend Overrides Function IsUniqueName(ByVal ruleName As String) As Boolean
            Return Not Me.declarativeConditionCollection.Contains(ruleName)
        End Function

        Protected Overrides Sub OnDeleteInternal(ByVal ruleObject As Object)
            Dim condition As RuleExpressionCondition = [TryCast](ruleObject,RuleExpressionCondition)
            Me.declarativeConditionCollection.Remove(condition.Name)
        End Sub

        Protected Overrides Function OnEditInternal(ByVal currentRuleObject As Object, <Out> ByRef updatedRuleObject As Object) As Boolean
            Dim condition As RuleExpressionCondition = [TryCast](currentRuleObject,RuleExpressionCondition)
            updatedRuleObject = Nothing
            [Using] dialog As RuleConditionDialog = New RuleConditionDialog(MyBase.Activity, condition.Expression)
                If (DialogResult.OK = dialog.ShowDialog(Me)) Then
                    updatedRuleObject = New RuleExpressionCondition(condition.Name, dialog.Expression)
                    Me.declarativeConditionCollection.Remove(condition.Name)
                    Me.declarativeConditionCollection.Add([TryCast](updatedRuleObject,RuleExpressionCondition))
                    Return True
                End If
            End [Using]
            Return False
        End Function

        Protected Overrides Function OnNewInternal() As Object
            [Using] dialog As RuleConditionDialog = New RuleConditionDialog(MyBase.Activity, Nothing)
                If (DialogResult.OK = dialog.ShowDialog(Me)) Then
                    Dim item As New RuleExpressionCondition { _
                        .Expression = dialog.Expression, _
                        .Name = Me.CreateNewName _
                    }
                    Me.declarativeConditionCollection.Add(item)
                    Return item
                End If
            End [Using]
            Return Nothing
        End Function

        Protected Overrides Function OnRenameInternal(ByVal ruleObject As Object) As String
            Dim item As RuleExpressionCondition = [TryCast](ruleObject,RuleExpressionCondition)
            [Using] dialog As RenameRuleObjectDialog = New RenameRuleObjectDialog(MyBase.Activity.Site, item.Name, New NameValidatorDelegate(AddressOf Me.IsUniqueName), Me)
                If ((dialog.ShowDialog(Me) = DialogResult.OK) AndAlso (dialog.RuleObjectName <> item.Name)) Then
                    Me.declarativeConditionCollection.Remove(item)
                    item.Name = dialog.RuleObjectName
                    Me.declarativeConditionCollection.Add(item)
                    Return dialog.RuleObjectName
                End If
            End [Using]
            Return Nothing
        End Function

        Protected Overrides Sub UpdateListViewItem(ByVal ruleObject As Object, ByVal listViewItem As ListViewItem)
            Dim condition As RuleExpressionCondition = [TryCast](ruleObject,RuleExpressionCondition)
            Dim service As ITypeProvider = DirectCast(MyBase.Activity.Site.GetService(GetType(ITypeProvider)), ITypeProvider)
            If (service Is Nothing) Then
                Throw New InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Messages.MissingService, New Object() { GetType(ITypeProvider).FullName }))
            End If
            Dim validation As New RuleValidation(MyBase.Activity, service, False)
            Dim flag As Boolean = condition.Validate(validation)
            listViewItem.Tag = condition
            listViewItem.Text = condition.Name
            Dim text As String = If(flag, Messages.Yes, Messages.No)
            If (listViewItem.SubItems.Count = 1) Then
                listViewItem.SubItems.Add([text])
            Else
                listViewItem.SubItems.Item(1).Text = [text]
            End If
        End Sub

        Protected Overrides Sub UpdatePreview(ByVal previewBox As TextBox, ByVal ruleObject As Object)
            Dim condition As RuleExpressionCondition = [TryCast](ruleObject,RuleExpressionCondition)
            If ((Not condition Is Nothing) AndAlso (Not condition.Expression Is Nothing)) Then
                Dim condition2 As New RuleExpressionCondition(condition.Expression)
                NativeMethods.SendMessage(previewBox.Handle, 11, IntPtr.Zero, IntPtr.Zero)
                previewBox.Lines = condition2.ToString.Split(New Char() { ChrW(10) })
                NativeMethods.SendMessage(previewBox.Handle, 11, New IntPtr(1), IntPtr.Zero)
                previewBox.Invalidate
            Else
                previewBox.Text = String.Empty
            End If
        End Sub


        ' Properties
        Protected Overrides ReadOnly Property ConfirmDeleteMessageText As String
            Get
                Return Messages.ConditionConfirmDeleteMessageText
            End Get
        End Property

        Protected Overrides ReadOnly Property ConfirmDeleteTitleText As String
            Get
                Return Messages.DeleteCondition
            End Get
        End Property

        Protected Overrides ReadOnly Property DescriptionText As String
            Get
                Return Messages.ConditionDescriptionText
            End Get
        End Property

        Friend Overrides ReadOnly Property DuplicateNameErrorText As String
            Get
                Return Messages.ConditionDuplicateNameErrorText
            End Get
        End Property

        Friend Overrides ReadOnly Property EmptyNameErrorText As String
            Get
                Return Messages.ConditionEmptyNameErrorText
            End Get
        End Property

        Friend Overrides ReadOnly Property NewNameLabelText As String
            Get
                Return Messages.ConditionNewNameLableText
            End Get
        End Property

        Protected Overrides ReadOnly Property PreviewLabelText As String
            Get
                Return Messages.ConditionPreviewLabelText
            End Get
        End Property

        Friend Overrides ReadOnly Property RenameTitleText As String
            Get
                Return Messages.ConditionRenameTitleText
            End Get
        End Property

        Protected Overrides ReadOnly Property TitleText As String
            Get
                Return Messages.ConditionTitleText
            End Get
        End Property


        ' Fields
        Private declarativeConditionCollection As RuleConditionCollection
    End Class
End Namespace

